import java.time.LocalDateTime;

/**
 * 内存可见性问题
 */
public class ThreadDemo17 {
    //全局变量(类级别)
    private volatile  static boolean flag=true;
    public static void main(String[] args) {
        //1.创建子线程1
        System.out.println("线程1开始执行"+ LocalDateTime.now());
        Thread t1=new Thread(()->{
            while(flag){
            }
            System.out.println("线程1结束执行！");
        });
        t1.start();
        Thread t2=new Thread(()->{
            //休眠1s
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("线程2修改 flag=false"+LocalDateTime.now());
            flag=false;
        });
        t2.start();
    }
}
